package com.newtouch.bxzs.business.message.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.newtouch.bxzs.business.message.mapper.AnnouncementMapper;
import com.newtouch.bxzs.business.message.model.AdvertisementBO;
import com.newtouch.bxzs.business.message.model.AnnouncementBO;
import com.newtouch.bxzs.business.message.model.AppHomeContentBO;
import com.newtouch.bxzs.business.message.service.AdvertisementService;
import com.newtouch.bxzs.business.message.service.AnnouncementService;
import com.newtouch.bxzs.business.message.service.AppHomeInitContentService;
import com.newtouch.bxzs.business.message.threadtask.AdvertisementTask;
import com.newtouch.bxzs.business.message.threadtask.AnnouncementTask;
import com.newtouch.bxzs.business.message.threadtask.KnowledgeCardClientTask;
import com.newtouch.bxzs.business.message.threadtask.ProductTask;
import com.newtouch.bxzs.business.product.model.ProductOnAppBO;
import com.newtouch.bxzs.business.product.service.ProductService;
import com.newtouch.bxzs.business.train.model.bo.KnowledgeCardAppBO;
import com.newtouch.bxzs.business.train.service.KnowledgeCardService;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * app首页内容Service实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/21 14:57</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class AppHomeInitContentServiceImpl implements AppHomeInitContentService {
    @Resource
    private AnnouncementService announcementService;

    @Resource
    private AnnouncementMapper announcementMapper;

    @Resource
    private AdvertisementService advertisementService;

    @Resource
    private ProductService productService;

    @Resource
    private KnowledgeCardService knowledgeCardService;

    /**
     * 获取app首页初始化内容
     *
     * @return AppHomeContentBO
     */
    @Override
    public AppHomeContentBO getAppHomeInitContent() {
        // 返回BO
        AppHomeContentBO appHomeContentBO = new AppHomeContentBO();
        List<AnnouncementBO> announcementBOList = new ArrayList<>();
        List<AdvertisementBO> advertisementBOList = new ArrayList<>();
        List<KnowledgeCardAppBO> knowledgeCardAppBOList = new ArrayList<>();
        List<ProductOnAppBO> productOnAppBOList = new ArrayList<>();

        // 获取当前登录用户的用户id
        String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
        // 根据用户id查询其app首页未读消息条数
        appHomeContentBO.setUnreadCount(announcementMapper.countUserUnReadNumber(userId));

        // 新建线程池
        ThreadPoolExecutor threadPoolExecutor = ThreadUtil.newExecutor(4, 8);
        List<Future<Object>> futureList = new ArrayList<>();
        futureList.add(threadPoolExecutor.submit(new ProductTask(productService)));
        futureList.add(threadPoolExecutor.submit(new KnowledgeCardClientTask(knowledgeCardService)));
        futureList.add(threadPoolExecutor.submit(new AnnouncementTask(announcementService)));
        futureList.add(threadPoolExecutor.submit(new AdvertisementTask(advertisementService)));
        // 获取远程服务返回结果
        for (Future<Object> future : futureList) {
            Object object = null;
            try {
                // 阻塞等待返回结果，设置超时时间3秒
                object = future.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
            // 判断返回值的类型
            if (object instanceof IPage) {
                List<?> records = ((IPage<?>) object).getRecords();
                if (!records.isEmpty()) {
                    for (Object record : records) {
                        // 如果是AnnouncementBO类型
                        if (record instanceof AnnouncementBO) {
                            announcementBOList.add((AnnouncementBO) record);
                        }
                        // 如果是AdvertisementBO类型
                        /*if (record instanceof AdvertisementBO) {
                            advertisementBOList.add((AdvertisementBO) record);
                        }*/
                        // 如果是KnowledgeCardBO类型
                        if (record instanceof KnowledgeCardAppBO) {
                            knowledgeCardAppBOList.add((KnowledgeCardAppBO) record);
						}
						// 如果是ProductOnAppBO类型
						if (record instanceof ProductOnAppBO) {
							productOnAppBOList.add((ProductOnAppBO) record);
						}
                    }
                    appHomeContentBO.setAnnouncementBO(announcementBOList);
//                    appHomeContentBO.setAdvertisementBO(advertisementBOList);
                    appHomeContentBO.setKnowledgeCardAppBO(knowledgeCardAppBOList);
                    appHomeContentBO.setProductOnAppBO(productOnAppBOList);
                }
            }
            if (object instanceof List) {
                List<?> advertisementList = ((List<?>) object);
                if (!advertisementList.isEmpty()) {
                    for (Object obj : advertisementList) {
                        // 如果是AdvertisementBO类型
                        if (obj instanceof AdvertisementBO) {
                            advertisementBOList.add((AdvertisementBO) obj);
                        }
                    }
                    appHomeContentBO.setAdvertisementBO(advertisementBOList);
                }
            }
        }

        // 返回
        return appHomeContentBO;
    }

}
